"""
Case Type   : GUC
Case Name   : 修改autovacuum_freeze_max_age，观察其预期结果
Create At   : 2020/11/18
Owner       : opentestcase018
Description :
    1、查询autovacuum_freeze_max_age默认值；
    show autovacuum_freeze_max_age；
    2、修改autovacuum_freeze_max_age为0，重启使其生效，观察预期结果；
    gs_guc set -D {cluster/dn1}  -c "autovacuum_freeze_max_age=100000"
    3、恢复默认值；
Expect      :
    1、显示默认值；
    2、修改成功并生效
    预期结果正常；
    3、恢复默认值成功；
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node

LOGGER = Logger()
COMMONSH = CommonSH('PrimaryDbUser')


class GuctestCase(unittest.TestCase):
    def setUp(self):
        LOGGER.info("==Guc_Autovacuum_Case0023开始执行==")
        self.constant = Constant()
        self.db_user_node = Node('PrimaryDbUser')
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_guc(self):
        LOGGER.info("查询autovacuum_freeze_max_age "
                    "期望：默认值4000000000")
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        LOGGER.info(sql_cmd)
        self.assertEqual("4000000000", sql_cmd.split("\n")[-2].strip())

        LOGGER.info("修改autovacuum_freeze_max_age为100000"
                    "重启生效，期望设置成功")
        result = COMMONSH.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                        'autovacuum_freeze_max_age=100000')
        self.assertTrue(result)

        LOGGER.info("期望：重启后查询结果为100000")
        COMMONSH.restart_db_cluster()
        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        LOGGER.info(sql_cmd)
        self.assertEqual("100000", sql_cmd.split("\n")[-2].strip())

        LOGGER.info('做DML')
        sql_cmd = COMMONSH.execut_db_sql('''
                drop table if exists test cascade;
                create table test(c_int int);
                begin
                    for i in 0..100 loop
                        insert into test values(i);
                        update test set c_int = 66 where c_int = i;
                    end loop;
                end;
                select count(*) from test;
                ''')
        LOGGER.info(sql_cmd)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], sql_cmd)

        LOGGER.info("恢复默认值")
        LOGGER.info("删除表")
        sql_cmd = COMMONSH.execut_db_sql(
            '''drop table if exists test cascade;''')
        LOGGER.info(sql_cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_cmd)
        result = COMMONSH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'autovacuum_freeze_max_age=4000000000')
        self.assertTrue(result)
        COMMONSH.restart_db_cluster()
        result = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in result or "Normal" in result)

    def tearDown(self):
        LOGGER.info("恢复默认值")
        LOGGER.info("删除表")
        sql_cmd = COMMONSH.execut_db_sql(
            '''drop table if exists test cascade;''')
        LOGGER.info(sql_cmd)
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        if "4000000000" != sql_cmd.split("\n")[-2].strip():
            COMMONSH.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                'autovacuum_freeze_max_age=4000000000')
            COMMONSH.restart_db_cluster()
        status = COMMONSH.get_db_cluster_status()
        result1 = COMMONSH.execute_gsguc('check', '4000000000',
                                         'autovacuum_freeze_max_age')
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertTrue(result1)
        LOGGER.info("==Guc_Autovacuum_Case0023执行结束==")
